package com.hopu.springbootdemo2.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * IOC容器工具类
 */
@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    //IOC容器
    private static ApplicationContext applicationContext;

    //回调方法 IOC容器创建后自动执行
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtils.applicationContext = applicationContext;
    }

    //返回容器中的对象
    public static <T> T getBean(Class<T> tClass){
        return applicationContext.getBean(tClass);
    }
}
